<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp" %>

<!DOCTYPE html>
<html>
<head>
	<title>添加调价计划明细</title>
	<%@ include file="/WEB-INF/component/commonCSS.jsp" %>
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1>添加调价计划明细</h1>
        <ol class="breadcrumb">
        	<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i>首页</a></li>
			<li class="active"><a href="#">交易</a></li>
			<li class="active"><a href="#">药品调价</a></li>
            <li class="active"><a href="#">未处理调价计划</a></li>
        </ol>
    </section>
    
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
            
            	<div class="input">
            		<div class="textalign1">通用名：</div>
            		<div class="boxinput">
            			<input id="drugName"  class="textinput" type="text" placeholder="通用名" />
            		</div>
					<div class="textalign1">药品编码：</div>
            		<div class="boxinput">
            			<input id="drugCode" class="textinput" type="text" placeholder="药品编码" />
            		</div>
            		<div class="textalign1">生产企业：</div>
            		<div class="boxinput">
            			<input id="prodComp" class="textinput" type="text" placeholder="生产企业" />
            		</div>
            		<div class="textalign1">
            			 <button type="button" class="btn btn-success btn-sm" onclick="search();" id="selectGoods">查     询 </button>
            		</div>
            	</div>
            </div>
        </div>
            
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
        	<label id="save" class="btn btn-default btn-sm" onclick="addDrug()">添加</label>
            <a href="${ctx}/wsjPriceAdjust/toPriceAdjPlanDetail.html?id=${pricePlan.priceAdjPlanId}" class="btn btn-default btn-sm">查看调价计划明细</a>
        </div>

    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    <script type="text/javascript">
    	
	    //绑定回车
	    $('#drugName,#prodComp,#drugCode').bind('keydown', function(event) {
	        if (event.keyCode == "13") {        //13 位电脑上回车键的位置代码
	            $('#selectGoods').click();
	        }
	    }); 
			
	    $("#drugName").focus();//加载页面光标在第一个查询框内
    
		function search(page) {
			var drugCode = $.trim($("#drugCode").val());
			var drugName = $.trim($("#drugName").val());
			var prodComp = $.trim($("#prodComp").val());
			$("#gridlist").jqGrid('setGridParam',{  
				mtype: "POST",
		        datatype:'json',
		        postData:{"drugCode":drugCode, "drugName":drugName, "prodComp":prodComp},
		        page : (page || 1)
		    }).trigger("reloadGrid"); //重新载入  
		}
		
		function addDrug() {
			var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
			if (rowIds.length <= 0) {
				$.HN.message.alert("请您选择要加入调价计划的商品！", "消息", "warn");
				return;
			}
			var list = [];
			var data = "";
			for (var i=0; i<rowIds.length; i++) {
				rowId = rowIds[i];
				var origProPriceLimit = $.trim($("#gridlist").jqGrid("getRowData",rowId).proPriceLimit);
				var currProPriceLimit = $.trim($("#newPurchasePrice"+rowId).val());
				if (currProPriceLimit == null || currProPriceLimit == "") {
					$.HN.message.alert("请输入新采购限价！", "消息", "warn");
	                return false;
				}
				if (!/^[1-9]\d*(\.\d+)?$/.test(currProPriceLimit)) {
	                $.HN.message.alert("新采购限价应该大于0的正数！", "消息", "warn");
	                return false;
	            }
				var index = currProPriceLimit.lastIndexOf(".");
	            if (index > 0 && currProPriceLimit.substr(index).length > 3) {
	                $.HN.message.alert("新采购价最多保留两位小数！", "消息", "warn");
	                return false;
	            }
	            if(parseFloat(origProPriceLimit)<=parseFloat(currProPriceLimit)){
	            	 $.HN.message.alert("新采购价必须小于采购限价！", "消息", "warn");
		                return false;
	            }
	            
				var origRetailPriceLimit = $.trim($("#gridlist").jqGrid("getRowData",rowId).retailPriceLimit);
				var currRetailPriceLlimit = $.trim($("#newHighestRetailPrice"+rowId).val());
				if (currRetailPriceLlimit == null || currRetailPriceLlimit == "") {
					$.HN.message.alert("请输入新最高零售价！", "消息", "warn");
	                return false;
				}
				if (!/^[1-9]\d*(\.\d+)?$/.test(currRetailPriceLlimit)) {
                    $.HN.message.alert("新最高零售价应该大于0的正数！", "消息", "warn");
                    return false;
                }
                var index = currRetailPriceLlimit.lastIndexOf(".");
                if (index > 0 && currRetailPriceLlimit.substr(index).length > 3) {
                    $.HN.message.alert("新最高零售价最多保留两位小数！", "消息", "warn");
                    return false;
                }
                if(parseFloat(origRetailPriceLimit)<=parseFloat(currRetailPriceLlimit)){
	            	 $.HN.message.alert("新最高零售价必须小于最高零售价！", "消息", "warn");
		                return false;
	            }
				list.push({
					"priceAdjPlanId":"${pricePlan.priceAdjPlanId}",
					"goodsCode":rowId,
					"origProPriceLimit": origProPriceLimit,
					"currProPriceLimit": currProPriceLimit,
					"origRetailPriceLimit": origRetailPriceLimit,
					"currRetailPriceLlimit": currRetailPriceLlimit
				});
				var goodsCode = $("#gridlist").jqGrid("getRowData",rowId).goodsCode;
				var drugName = $("#gridlist").jqGrid("getRowData",rowId).drugName;
				data += "<br/>"+goodsCode + "/" +drugName;
			}
			$.HN.message.confirm("您确定以下商品加入调价计划吗？"+data, "提示信息", "确定").on(function(bool) {
				if (bool) {
			       	$.ajax({
						type:"POST",
						url:"${ctx}/wsjPriceAdjust/addDetail.html",
						data:{"drugs":JSON.stringify(list)},
						dataType:"json",
						success:function(result){
							if (result.success) {
								search();
								$.HN.message.alert("为您成功添加"+result.operCount+"条明细！", "消息", "success");
			    			} else {
			    				$.HN.message.alert("操作失败！", "消息", "error");
			    			}
						},
						error:function() {
							top.window.document.location = "${ctx}/login.html";
						}
					});
				}
			});
		}
		
		$(function () {
            $("#gridlist").jqGrid({
                url: "${ctx}/wsjPriceAdjust/getPriceAdjPlanDurgList.html",
                mtype: "POST",
                postData:{"priceid":"${pricePlan.priceAdjPlanId}"},
                datatype: "json",
                autowidth: true,
                shrinkToFit : true,
             //   height: 270,
                multiselect: true,
                colNames: ['药品编码', '通用名',  '剂型', '规格', '转换系数', '单位', '材质','生产企业','采购限价', '新采购限价', '最高零售限价', '新最高零售限价', '所属项目','商品编码','生产企业ID','药品ID'],
                colModel: [
					
					{ name: 'drugCode', width: 100,align: 'center', sortable:false,
					    formatter:function(cellvalue, options, row) {
					    	var str = "<a href=\"javascript:void(0)\" onclick=\"showDrugInfo('"+row.drugCode+"','"+row.drugName+"')\">"+cellvalue+"</a>";
                    		return str;
					    }
       	            },
                    { name: 'drugName', width: 140,align: 'left', sortable:false },
                    { name: 'formName', width: 80 ,align: 'center', sortable:false },
                    { name: 'specName', width: 80 ,align: 'left', sortable:false },
                    { name: 'factor', width: 80,align: 'center', sortable:false },
                    { name: 'unit', width: 40 ,align: 'center', sortable:false },
					{ name: 'materialName', width: 80 ,align: 'center', sortable:false },
                    { name: 'prodComp', width: 180,align: 'center', sortable:false,
						formatter:function(cellvalue, options, row) {
							var str =  "<a href=\"javascript:void(0)\" onclick=\"showCompInfo('"+row.prodCompId+"', '"+row.compName+"');\">"+cellvalue+"</a>";
                    		return str;
						}
					},
                    { name: 'proPriceLimit', width: 80, align: "right", sortable:false,
						formatter:function(cellvalue, options, row){ 
                        	return formatAmount(cellvalue, options, row);
                        	} 	
					},
                    { name: 'newPurchasePrice', width: 80, align: "right", sortable:false, formatter : function(val, opts, rowdata) {
                    	var str = "<input type='text' maxlength='10' oninput=\"value=value.replace(/[^\\d{1,}\\.\\d{1,}|\\d{1,}]/g,'')\" id='newPurchasePrice"+rowdata.goodsCode+"' style='width:99%;text-align:right;' />";
                    	return  str;
                    } },
                    { name: 'retailPriceLimit', width: 80, align: "right", sortable:false,
                    	formatter:function(cellvalue, options, row){ 
                        	return formatAmount(cellvalue, options, row);
                        	} },
                    { name: 'newHighestRetailPrice', width:80, align: "right", sortable:false, 
						formatter : function(val, opts, rowdata) {
							var str = "<input type='text' maxlength='10' oninput=\"value=value.replace(/[^\\d{1,}\\.\\d{1,}|\\d{1,}]/g,'')\" id='newHighestRetailPrice"+rowdata.goodsCode+"' style='width:99%;text-align:right;' />";
							return str;
						}
					},
                    
                    { name: 'projName', width: 160,align: 'left', sortable:false,hidden:true },
                    { name: 'goodsCode', width: 100,align: 'center', sortable:false,hidden:true },
					{ name: 'prodCompId',index: 'prodCompId', hidden:true, width: 120,align: 'center'},
					{ name: 'drugId',index: 'drugId', hidden:true, width: 120,align: 'center'}
                ],
                rowNum: 20,
                rowList: [10, 20, 50, 100],
                rownumbers: true,
                pager: "#gridpage",
                viewrecords: true,
                caption: '${pricePlan.priceAdjPlanName} ( ${pricePlan.executeCat} ${pricePlan.executeDateTime} )',
                jsonReader: {
                    repeatitems: false,
                    id: "goodsCode"
                },
                gridComplete:function(){
                    //序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                    //数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
			$("#gridlist").jqGrid("setGridHeight",create());
        });
 
    </script>
</body>
</html>